package org.wevil.anno;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.wevil.util.NumberUtil;

import java.io.IOException;
import java.util.Objects;

/**
 * 数字格式化序列化
 */
public final class NumberFormatSerializer extends JsonSerializer<Number> {

    int scale;
    boolean rightZero;
    boolean comma;

    public NumberFormatSerializer(int scale, boolean rightZero, boolean comma) {
        super();
        this.scale = scale;
        this.rightZero = rightZero;
        this.comma = comma;
    }

    @Override
    public void serialize(Number value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(!Objects.isNull(value)){
            jsonGenerator.writeString(NumberUtil.format(value, scale, rightZero, comma));
        }
    }
}
